/**
 * @file StateGame.cpp
 * 
 * @author José Tomás Tocino García
 * @date 2011
 *
 * Copyright (C) 2011 José Tomás Tocino García <theom3ga@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */


#include "StateGame.h"
#include "log.h"
#include "ResourceManager.h"
#include "i18n.h"

StateGame::StateGame(){
    lDEBUG << Log::CON("StateGame");
}
StateGame::~StateGame(){
    lDEBUG << Log::DES("StateGame");
}

void StateGame::loadResources(){

    // Load the score board
    scoreBoard.loadResources();

    // Load the time board
    timeBoard.loadResources();

    // Load the background image
    spBoard.SetImage(ResMgr -> getImage("media/board.png"));

    // Load the image for the square selector
    spSelector.SetImage(ResMgr -> getImage("media/selector.png"));    

}

void StateGame::event (sf::Event theEvent){
    // If a key is pressed
    if (theEvent.Type == sf::Event::KeyPressed){

        // If escape was pressed
        if (theEvent.Key.Code == sf::Key::Escape){
            pManager -> popState();
        } 
    }
    /*
    else if(theEvent.Type == sf::Event::MouseMoved){
        int mY = (int) theEvent.MouseMove.Y;

        if(mY >= menuYStart && mY < menuYEnd){
            menuSelectedOption = (mY - menuYStart) / menuYGap;
        }
    }

    else if (theEvent.Type == sf::Event::MouseButtonReleased){
        int mY = (int) theEvent.MouseMove.Y;

        if(mY >= menuYStart && mY < menuYEnd){
            optionChosen();
        }
    } //*/
}

void StateGame::update (bool isCovered){

}

void StateGame::draw (bool isCovered, DrawingQueue& queue){
    queue.Draw(0, spBoard);

    scoreBoard.draw (queue);
    timeBoard.draw (queue);
}

/// Tests if the mouse is over a gem
bool StateGame::overGem (int mX, int mY){
    return (mX > 241 && 
            mX < 241 + 65 * 8 &&
                 mY > 41 && 
            mY < 41 + 65 * 8);
}

/// Returns the coords of the gem the mouse is over
coord StateGame::getCoord (int mX, int mY){
    return coord((mX - 241) / 65 , 
                 (mY - 41) / 65 );
}

/// Creates a small label that indicates the points generated by a match
void StateGame::createFloatingScores(){

}
    
/// Check if that gem was clicked
bool StateGame::checkClickedSquare(int mX, int mY){

    // Get coordinates for the square the mouse is over
    selectedSquareSecond = getCoord(mX, mY);

    bool returnValue = false;

    // If both squares are next to each other horizontally or vertically
    if(abs(selectedSquareFirst.x - selectedSquareSecond.x) +
       abs(selectedSquareFirst.y - selectedSquareSecond.y) == 1){ 

        // Create a temporal board
        Board temporal = board;

        // Make the movement in the temporal board
        temporal.swap(selectedSquareFirst.x, selectedSquareFirst.y,
                      selectedSquareSecond.x, selectedSquareSecond.y);

        // Check if the new movement would cause a match
        groupedSquares = temporal.check();

        // If there's a match
        if(! groupedSquares.empty()){

            // Switch the state
            state = eGemasCambiando;

            // Return true
            returnValue = true;
        }else{

            // Return false
            returnValue = false;
        }
    }

    return returnValue;
}

/// Shows a hint for a possible match
void StateGame::showHint(){

}

/// Plays the sound for the match
void StateGame::playMatchSound(){

}

/// Resets the game
void StateGame::resetGame(){

}
